<?php

namespace app\api\logic;

use app\basic\BaseLogic;
use app\admin\model\article\Article;

class ArticleLogic extends BaseLogic
{
     /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new Article();
    }

     /**
     * 获取文档详情
     * @param int $id 文档ID
     * @return array|null
     */
    public function getArticleDetail($id,$userId)
    {
        $detail = $this->model->alias('a')
            ->join('article_category c', 'a.category_id = c.id')
            ->field('a.*, c.category_name')
            ->where('a.id', $id)
            ->where('a.status', 1)
            ->find();
        if ($detail) {
            $detail = $detail->toArray();
            // 记录浏览足迹
            if($userId){
               $this->recordFootprint($detail,$userId);
            }
            return $detail;
        }
        return null;
    }

     /**
     * 记录浏览足迹
     * @param array $article 文章信息
     */
    protected function recordFootprint($article,$userId)
    {
        $footprintLogic = new \app\api\logic\UserFootprintLogic();
        $footprintLogic->addFootprint($userId, $article['id'], $article['title']);
    }

    /**
     * 是否收藏
     * @return void
     */
    public function isCollect($userId, $id)
    {
        if($userId){
            if ((new UserCollectionLogic())->isCollected($userId, $id)) {
                return "1";
            }
        }
        return 0;
    }

}